//
//  SLNearlyBoatDetailViewModel.m
//  StarLight
//
//  Created by Duke.wu on 16/5/19.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLNearlyBoatDetailViewModel.h"
#import "SLNetWork.h"

@interface SLNearlyBoatDetailViewModel ()

@property (nonatomic, strong) NSString *lxfs;

@end

@implementation SLNearlyBoatDetailViewModel


#pragma  mark - Detail

- (instancetype)initWithBid:(NSString *)bid{
  if (self = [super init]) {
    [self getDetailWithBid:bid];
  }
  return self;
}


- (void)getDetailWithBid:(NSString *)bid{
   NSString *requst = [NSString stringWithFormat:@"%@/boat/boatallinfo?bid=%@",baseUrl,bid];
  
  
  @weakify(self)
  [SLNetWork getDataWithRequest:requst complete:^(id respond, NSError *error) {
  @strongify(self)
    if (!error) {
      
      self.model = [SLBoatsDetailModel mj_objectWithKeyValues:respond[@"data"]];
      [self connectToShipOwner:respond[@"data"][@"shiponwerId"]];
    }
  }];
}


- (void)connectToShipOwner:(NSString *)shipOwnerId{
  
  NSString *requst = [NSString backUrlWithService:@"boat" methodName:@"contactship"];
  if(shipOwnerId != nil){
    NSDictionary *para = @{@"hid":shipOwnerId,@"uid":[SLNetWork backUid]};
    @weakify(self)
    [SLNetWork postDataWithParameters:para postUrl:requst complete:^(id respond, NSError *error) {
      if (!error) {
        @strongify(self)
        self.lxfs = respond[@"lxfs"];
      }
    }];
 
  }
}

- (void)connectShipOwner{
  
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.lxfs]]];
}


#pragma  mark - More

- (instancetype)initWithHid:(NSString *)hid{
  if (self = [super init]) {
    [self getMoreBoatsWithHid:hid];
  }
  return self;
}

- (void)getMoreBoatsWithHid:(NSString *)hid{
  @weakify(self)
  NSString *requst = [NSString stringWithFormat:@"%@/boat/userboats?hid=%@&uid=%@",baseUrl,hid,[SLNetWork backUid]];
  [SLNetWork getDataWithRequest:requst complete:^(id respond, NSError *error) {
    @strongify(self)
    if (!error) {
      self.moreModel = [SLBoatMoreModel mj_objectWithKeyValues:respond[@"data"]];
    }
  }];

}

- (void)refreshWithHid:(NSString *)hid{
  [self getMoreBoatsWithHid:hid];
}



@end


@implementation SLBoatsDetailModel

//+ (NSDictionary *)mj_replacedKeyFromPropertyName{
//  return @{@"bid":@"id"};
//}

@end

@implementation SLBoatMoreModel

+ (NSDictionary *)mj_objectClassInArray{
  return @{@"boats":[SLNearLyShip class]};
}

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
  return @{@"shipOwnerId":@"id"};
}

@end





